package com.pitaya.winwine.repository.spec;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;

import com.pitaya.winwine.domain.Wine;

public class WineSpec {

	public static Specification<Wine> findExist(final Wine wine) {
		return new Specification<Wine>() {
			@Override
			public Predicate toPredicate(Root<Wine> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				return query.where(
					builder.and(
						builder.equal(root.get("vintage"), wine.getVintage()),
						builder.equal(root.get("capacity"), wine.getCapacity())
					),
					builder.or(
						builder.equal(root.get("enName"), wine.getEnName()),
						builder.equal(root.get("chName"), wine.getChName())
					)
				).getGroupRestriction();
			}
		};
	}
}
